<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Finance;
use App\Traits\ValidationTrait;
use Illuminate\Http\Request;

class FinanceController extends Controller
{
    use ValidationTrait;

    public function __construct()
    {
        view()->share('menu', 'sys');
    }

    public function index()
    {
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $fina = Finance::find(1);
        if ($fina) {
            $data['s_head']     = explode(':', $fina->start_time)[0];
            $data['s_end']      = explode(':', $fina->start_time)[1];
            $data['e_head']     = explode(':', $fina->end_time)[0];
            $data['e_end']      = explode(':', $fina->end_time)[1];
            $data['min']        = $fina->min;
            $data['max']        = $fina->max;
            $data['proc']       = $fina->proc;
            $data['scale']      = $fina->scale;
            $data['out_time']   = $fina->out_time;
            $data['total_buy']  = $fina->total_buy;
            $data['thaw_scale'] = $fina->thaw_scale;
        } else {
            $data['s_head']     = '' ;
            $data['s_end']      = '' ;
            $data['e_head']     = '' ;
            $data['e_end']      = '' ;
            $data['min']        = '' ;
            $data['max']        = '' ;
            $data['proc']       = '' ;
            $data['scale']      = '' ;
            $data['out_time']   = '' ;
            $data['total_buy']  = '' ;
            $data['thaw_scale'] = '' ;
        }

        return view('admin/finance/create', compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //验证
        $validator = $this->verify($request, 'validation.finance_create');
        if ($validator->fails()) {
            $messages = $validator->messages()->first();
            return back()->with(['msg' => $messages])->withInput();
        }

        $s_head     = $request->get('s_head');
        $s_end      = $request->get('s_end');
        $e_head     = $request->get('e_head');
        $e_end      = $request->get('e_end');
        $min        = $request->get('min');
        $max        = $request->get('max');
        $proc       = $request->get('proc');
        $scale      = $request->get('scale');
        $out_time   = $request->get('out_time');
        $total_buy  = $request->get('total_buy');
        $thaw_scale = $request->get('thaw_scale');
        Finance::updateOrCreate(
            [
                'id' => 1,
            ],
            [
                'start_time' => $s_head . ":" . $s_end,
                'end_time'   => $e_head . ":" . $e_end,
                'min'        => $min,
                'max'        => $max,
                'proc'       => $proc,
                'scale'      => $scale,
                'out_time'   => $out_time,
                'total_buy'  => $total_buy,
                'thaw_scale' => $thaw_scale,
            ]
        );

        return back()->with(['msg' => '修改成功', 'status' => 1]);
    }

    public function show($id)
    {
    }

    public function edit($id)
    {
    }

    public function update(Request $request, $id)
    {
    }

    public function destroy($id)
    {
    }
}
